﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Proxy;
using EFong.Contract;

namespace EFong.View.Financing
{
    public partial class Frm_CustomerSettlement : BasicMdiChildForm
    {
        private Proxy.FinanceServiceProxy _financeServiceProxy { get { return Proxy.FinanceServiceProxy.GetInstance(); } }
        private Proxy.MasterServiceProxy _masterProxy {get{ return Proxy.MasterServiceProxy.GetInstance();}}
        private Proxy.BaseServiceProxy _baseServiceProxy { get { return Proxy.BaseServiceProxy.GetInstance(); } }

        private List<Order> orderList = null;

        private Contract.Customer _customer;
        public Contract.Customer Customer
        {
            get { return _customer; }
            set
            {
                _customer = value;
                onCustomerChanged();
            }
        }

        public Frm_CustomerSettlement()
        {
            InitializeComponent();
            cmb_BankAccount.ReadOnly(true);
        }

        protected override void InitFields()
        {
        }

        protected override void LoadDatas()
        {
            //this.cmb_Customer.Properties.BindingData(_masterProxy.GetAllCustomer().ReturnValue);
            bds_Customer.BindingData(_masterProxy.GetAllCustomer().ReturnValue);
            this.cmb_BankAccount.Properties.BindingData(_masterProxy.GetAllBankAccount().ReturnValue);
            this.dte_PostingDate.DateTime = DateTime.Now;
            this.dte_SettlementDate.DateTime = DateTime.Now;

        }

        protected override void Events()
        {
            this.hpl_SelectAll.Click += (s, e) =>
                {
                    if (orderList == null)
                        return;

                    foreach (Order order in orderList)
                        order.IsSelect = true;

                    refreshSelect();
                };

            this.hpl_SelectNone.Click += (s, e) =>
            {
                if (orderList == null)
                    return;

                foreach (Order order in orderList)
                    order.IsSelect = false;

                refreshSelect();
            };

            this.hpl_SelectRevert.Click += (s, e) =>
            {
                if (orderList == null)
                    return;

                foreach (Order order in orderList)
                    order.IsSelect = !order.IsSelect;

                refreshSelect();
            };

            this.cmb_Customer.EditValueChanged += (s, e) =>
            {
                loadCustomer();
            };

            this.gv_Orders.CellValueChanged += (s, e) =>
            {
                refreshSelect();
            };

            this.cmb_PayType.SelectedIndexChanged += (s, e) =>
            {
                if (ViewState == ViewStates.EDIT) { return; }

                this.cmb_BankAccount.SetValue(default(Contract.BankAccount));
                this.cmb_BankAccount.ReadOnly(this.cmb_PayType.SelectedIndex <= 0);
            };

            this.cmb_BankAccount.SelectedIndexChanged += (s, e) =>
            {
                Contract.BankAccount bankAccount = cmb_BankAccount.GetValue() as Contract.BankAccount;
                if (bankAccount == null)
                    txt_BankAccountAmount.Text = "0";
                else
                    txt_BankAccountAmount.Text = string.Format("{0} {1}", bankAccount.Currency.Code, bankAccount.Amount);
            };
        }

        private void refreshSelect()
        {
            decimal totalReceive = 0;
            decimal totalIntergral = 0;
            foreach (Order order in orderList)
            {
                if (!order.IsSelect)
                    continue;

                totalIntergral += order.Intergral;
                totalReceive += order.Money;
            }

            this.txt_TotalAccountReceivable.Text = totalReceive.ToString();
            this.txt_TotalIntergral.Text = totalIntergral.ToString();

            grd_Orders.RefreshDataSource();
            this.cmb_BankAccount.Properties.BindingData(_masterProxy.GetAllBankAccount().ReturnValue);
        }

        void loadCustomer()
        {
            RCustomer rCustomer = this.cmb_Customer.GetValue() as RCustomer;
            if (rCustomer == null)
                return;
            Customer = _masterProxy.GetCustomerByID(rCustomer.ID).ReturnValue;
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Save":
                    this.save();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        private void save()
        {
            CustomerSettlement customerSettlement = new CustomerSettlement();
            customerSettlement.Amount = txt_Amount.GetValue();
            customerSettlement.BankAccount = cmb_BankAccount.GetValue() as Contract.BankAccount;
            customerSettlement.CheckNo = txt_CheckNo.GetText();
            customerSettlement.Customer = Customer;
            customerSettlement.Intergral = txt_UseIntergral.GetValue();
            customerSettlement.OrderList = (from o in orderList
                                            where o.IsSelect
                                            select o).ToList();
            customerSettlement.PaymentType = cmb_PayType.GetValue() as string;
            customerSettlement.PostingDate = dte_PostingDate.GetValue(DateTime.MinValue);
            customerSettlement.Remark = txt_Remark.GetText();
            customerSettlement.SettlementDate = dte_SettlementDate.GetValue(DateTime.MinValue);

            if (_financeServiceProxy.CustomerSettle(customerSettlement).IsSuccess) 
            {
                MsgBox.Info("保存成功");
                loadCustomer();
                refreshSelect();
            }
        }


        void onCustomerChanged()
        {
            if (Customer == null)
            {
                // clear customer datas
                this.txt_CustomerIntergral.Text = string.Empty;

                this.grd_Orders.DataSource = null;
            }
            else
            {
                this.txt_CustomerIntergral.Text = Customer.Intergral.ToString();
                this.txt_PrePay.Text = Customer.Amount.ToString();
                orderList = _financeServiceProxy.GetOrdersByCustomer(Customer).ReturnValue;
                grd_Orders.DataSource = orderList;
            }
        }

        private void btn_AllCustomer_Click(object sender, EventArgs e)
        {
            Customer = null;
        }
    }
}
